package com.wzdigit.wms.wms.controller.TransferWareHouse;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.transfer.GdnCtnrCommitRequest;
import com.wzdigit.wms.basic.client.request.transfer.GdnCtnrRequest;
import com.wzdigit.wms.basic.client.request.transfer.SnAndQtyRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.transfer.GdnCtnrDtlGoodsResponse;
import com.wzdigit.wms.basic.client.response.transfer.GdnCtnrResponse;
import com.wzdigit.wms.basic.client.response.transfer.GdnCtnrTaskResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.dto.inventory.GoodsDto;
import com.wzdigit.wms.wms.service.transfer.GdnCtnrService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

;

/**
 * @ClassName GdnCtnrController
 * @Description 发货装运PDA
 * @Author LHR-PC
 * @Date 2021/3/4 14:21
 * @Version
 **/
@RestController
@Api(tags = "发货装运PDA")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class GdnCtnrController {

    public static final String REQUEST_MODEL = "/gdnCtnr";

    @Autowired
    private GdnCtnrService gdnCtnrService;

//    1)	进入发货装运任务页面，显示待发货装运的信息

    /**
     * 2.1.1【发货单号】查询
     * 根据发货单号，查询发货单(WMS_GDN_HEADER)，
     * 并且发货单明细(WMS_GDN_DTL)存在状态(STATUS)等于 待装柜(5)、装柜中(6)的数据。
     */

    @ApiOperation(value = "发货装运任务主页",notes = "发货装运任务主页")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/list")
    public PagerInfo<List<GdnCtnrTaskResponse>> list(
            GdnCtnrRequest gdnCtnrRequest,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return gdnCtnrService.list(gdnCtnrRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "发货单号列表",notes = "发货号单列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/gdnList")
    public Result<List<String>> gdnList(
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return gdnCtnrService.gdnList();
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "参考柜号列表",notes = "参考柜号列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/refCntrNo")
    public Result<List<String>> refCntrNoList(
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return gdnCtnrService.refCntrNoList();
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "根据发货单号查询发货装货页面",notes = "根据发货单号查询发货装货页面")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/{gdnNo}")
    public Result<GdnCtnrResponse> gdnVo(
            @PathVariable("gdnNo") String gdnNo,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return gdnCtnrService.selectGdnVoByGdnNo(gdnNo);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "根据发运单明细ID查柜号列表",notes = "根据发运单明细ID查柜号列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL +  "/ctnrList/{dtlId}")
    public Result<List<String>> ctnrNoList(
            @PathVariable("dtlId") Long dtlId,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return gdnCtnrService.selectCtnrNoListByDtlId(dtlId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "根据明细ID和柜号查询发货装运明细",notes = "根据明细ID和柜号查询发货装运明细")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL +  "/gdnCtnrDtl")
    public Result<GdnCtnrDtlGoodsResponse> ctnrDtl(
            @RequestParam(value = "dtlId",required = true) Long dtlId,
            @RequestParam(value = "ctnrNo",required = false) String ctnrNo,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return gdnCtnrService.selectCtnrDtlByCtnrNo(dtlId,ctnrNo);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "校验SN是否可扫",notes = "校验SN是否可扫")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL +  "/checkSn")
    public Result<SnAndQtyRequest> checkSn(
            @RequestParam(value = "sn",required = true) String sn,
            @RequestParam(value = "dtlId",required = true) Long dtlId,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return gdnCtnrService.checkSn(sn,dtlId,false);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "数量转移",notes = "数量转移")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL +  "/countChange/{sn}/{dtlId}")
    public Result<Integer> countChange(
            @ApiParam("条码")@PathVariable(value = "sn") String sn,
            @ApiParam("明细ID")@PathVariable(value = "dtlId") Integer dtlId,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return gdnCtnrService.countChange(sn,dtlId,servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "提交",notes = "提交")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL +  "/commit")
    public Result<?> commit(
            @RequestBody GdnCtnrCommitRequest gdnCtnrCommitRequest,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) throws Exception {
        try {
            return gdnCtnrService.commit(gdnCtnrCommitRequest, HttpSessionUtil.getLoginName(servletRequest));
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "结束装柜",notes = "结束装柜")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL +  "/finish")
    public Result<?> finish(
            @RequestBody GdnCtnrCommitRequest gdnCtnrCommitRequest,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) throws Exception {
        try {
            return gdnCtnrService.finish(gdnCtnrCommitRequest, HttpSessionUtil.getLoginName(servletRequest));
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "检查是否满足关单条件",notes = "检查是否满足关单条件")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL +  "/{gdnNo}")
    public Result<GdnCtnrTaskResponse> checkIsClose(
            @PathVariable("gdnNo") String gdnNo,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return gdnCtnrService.checkIsClose(gdnNo);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "结束装运并关单",notes = "结束装运并关")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL +  "/close")
    public Result<?> close(
            @RequestParam String gdnNo,
            @RequestParam Integer isCreateRemainNote,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return gdnCtnrService.close(gdnNo,isCreateRemainNote, HttpSessionUtil.getLoginName(servletRequest));
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "结束装运并关单前校验",notes = "结束装运并关单前校验")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL +  "/close_before/{gdnNo}/{ctnrNo}")
    public Result<?> closeBefore(
            @PathVariable @ApiParam("发货单号") String gdnNo,
            @PathVariable @ApiParam("柜号") String ctnrNo,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return gdnCtnrService.closeBefore(gdnNo,ctnrNo, HttpSessionUtil.getLoginName(servletRequest));
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }


    @ApiOperation(value = "结束装运并关单前查询发运状态",notes = "结束装运并关单前查询发运状态")
    @GetMapping(value = REQUEST_MODEL +  "/close/before/select")
    public Result<?> closeBeforeSelect(
            @RequestParam String gdnNo,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return gdnCtnrService.closeBeforeSelect(gdnNo, HttpSessionUtil.getLoginName(servletRequest));
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "查询缓存,已扫描但未提交的SN",notes = "缓存已扫描但未提交的SN")
    @GetMapping(value = REQUEST_MODEL +  "/getSnCacheList/{dtlId}/{gdnNo}")
    public Result<List<GoodsDto>> getSnCacheList(
            @ApiParam("明细ID") @PathVariable("dtlId") Long dtlId,
            @ApiParam("发货单单号") @PathVariable("gdnNo") String gdnNo,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            List<GoodsDto> goodsDtoList = gdnCtnrService.getSnCacheList(dtlId, gdnNo);

            Result<List<GoodsDto>> result = new Result<>();
            result.setData(goodsDtoList);
            result.setFlag(!(goodsDtoList == null));
            return result;
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }


    @ApiOperation(value = "删除缓存记录",notes = "删除缓存记录")
    @DeleteMapping(value = REQUEST_MODEL +  "/deleteSnCacheList/{dtlId}/{gdnNo}/{snList}")
    public Result deleteSnCacheList(
            @ApiParam("明细ID") @PathVariable("dtlId") Long dtlId,
            @ApiParam("发货单单号") @PathVariable("gdnNo") String gdnNo,
            @ApiParam("SN") @PathVariable("snList") List<String> snList,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            gdnCtnrService.deleteSnCacheList(dtlId,gdnNo,snList);
//            gdnCtnrService.deleteSnCacheList(dtlId,gdnNo);
            return new Result();
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    //TODO 后续发布
    @ApiOperation(value = "回传TMS",notes = "回传TMS")
    @PostMapping(value = REQUEST_MODEL +  "/backTms/{gdnHeaderId}")
    public String  backTms(
            @ApiParam("主档ID") @PathVariable("gdnHeaderId") BigDecimal gdnHeaderId,
            @ApiParam("明细ID集合") @RequestBody List<BigDecimal> gdnDtlIdList,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) throws Exception{
//            gdnCtnrService.deleteSnCacheList(dtlId,gdnNo);
        try {
            return gdnCtnrService.tmsToOms(gdnHeaderId,gdnDtlIdList);
        } catch (Exception e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }

    }

}
